package com.mokee.widget.LargeImageView;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * 
	1. setImagePath:获得图片的真实的宽度和高度，以及初始化mDecoder

	2. onMeasure:显示区域的rect赋值，大小为view的尺寸

	3. onTouchEvent:监听move的手势，在监听的回调里面去改变rect的参数，以及做边界检查，最后invalidate

	4. onDraw:根据rect拿到bitmap，draw

 * @author Mokee
 *
 */
public class LargeImageView extends View {
	public static final String tag = "RegionImageView";
	
	/** 区域解码器 */
	private BitmapRegionDecoder mDecoder;
	private MoveGestureDetector mDetector;
	/** 图片高度和宽度 */
	private int mImageWidth, mImageHeight;
	/** 绘制区域 */
	private volatile Rect mRect = new Rect();
	/** 解码选项 */
	private static final BitmapFactory.Options mOptions = new BitmapFactory.Options();
	
	static {
		mOptions.inPreferredConfig = Config.RGB_565;
	}

	public void init() {
		mDetector = new MoveGestureDetector(getContext(), new MoveGestureDetector.SimpleMoveGestureDetector() {
					@Override
					public boolean onMove(MoveGestureDetector detector) {
						int moveX = (int) detector.getMoveX();
						int moveY = (int) detector.getMoveY();
						
						if (mImageWidth > getWidth()) {
							mRect.offset(-moveX, 0);
							checkWidth();
							invalidate();
						}
						if (mImageHeight > getHeight()) {
							mRect.offset(0, -moveY);
							checkHeight();
							invalidate();
						}
						Log.i(tag, "Rect：	Left:" + mRect.left + ",	Right:" + mRect.right + ",	Top:" + mRect.top + ",	Bottom:" + mRect.bottom);
						return true;
					}
				});
	}
	
	private void checkWidth() {
		Rect rect = mRect;
		int imageWidth = mImageWidth;
		if (rect.right > imageWidth) {
			rect.right = imageWidth;
			rect.left = imageWidth - getWidth();
		}
		if (rect.left < 0) {
			rect.left = 0;
			rect.right = getWidth();
		}
	}

	private void checkHeight() {
		Rect rect = mRect;
		int imageHeight = mImageHeight;
		if (rect.bottom > imageHeight) {
			rect.bottom = imageHeight;
			rect.top = imageHeight - getHeight();
		}
		if (rect.top < 0) {
			rect.top = 0;
			rect.bottom = getHeight();
		}
	}

	/**
	 * 获得图片真实的宽度和高度，及初始化mDecoder
	 * @param path
	 */
	public void setImagePath(String path) {
		try {
			mDecoder = BitmapRegionDecoder.newInstance(path, false);
			
			/*****************************获取图片的长宽***************************/
			BitmapFactory.Options tempOptions = new BitmapFactory.Options();
			tempOptions.inJustDecodeBounds = true;
			BitmapFactory.decodeFile(path, tempOptions);
			mImageWidth = tempOptions.outWidth;
			mImageHeight = tempOptions.outHeight;
			
			Log.i(tag, "mImageWidth:" + mImageWidth + ",	mImageHeight:" + mImageHeight);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
		}
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		Bitmap bitmap = mDecoder.decodeRegion(mRect, mOptions);
		canvas.drawBitmap(bitmap, 0, 0, null);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		
		int width = getMeasuredWidth();
		int height = getMeasuredHeight();

		/** 显示开始部分 */
		mRect.left = 0;
		mRect.top = 0;
		mRect.right = mRect.left + width;
		mRect.bottom = mRect.top + height;

	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		mDetector.onTouchEvent(event);
		return true;
	}

	public LargeImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
}
